//
//  RecommendNewsCollectionViewCell.swift
//  xmly
//
//  Created by 刘文超 on 2019/12/30.
//  Copyright © 2019 刘文超. All rights reserved.
//

import UIKit

class RecommendNewsCollectionViewCell: UICollectionViewCell {
    
    lazy var leftImageView:UIImageView  = {
        let imageView = UIImageView.init()
        imageView.image = UIImage.init(named: "news")
        imageView.backgroundColor = arc4randomColor
        return imageView
    }()
    
    lazy var rightButton: UIButton = {
        let button = UIButton.init()
        button.setTitle("| 更多", for: .normal)
        button.setTitleColor(UIColor.gray, for: .normal)
        button.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        button.backgroundColor = UIColor.clear
        return button
    }()
    
    lazy var collectionView:UICollectionView = {
        let  flowlayout = UICollectionViewFlowLayout.init()
        flowlayout.minimumLineSpacing = 0
        flowlayout.minimumInteritemSpacing = 0
        flowlayout.itemSize = CGSize.init(width: ScreenWidth-150, height: 40)
        let collectionView = UICollectionView.init(frame: CGRect.init(x: 80, y: 5, width: ScreenWidth-150, height: 40), collectionViewLayout: flowlayout)
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.showsVerticalScrollIndicator = false
        collectionView.isPagingEnabled = false
        collectionView.backgroundColor = UIColor.white
        collectionView.isScrollEnabled = false
        collectionView.register(NoticCell.self, forCellWithReuseIdentifier: "cell")
        return collectionView
    }()
    
    var timer:Timer?
    
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setLayout()
        
        //开启定时器
        startTimer()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    func setLayout() {
        self.addSubview(leftImageView)
               leftImageView.snp.makeConstraints { (make) in
                   make.left.equalToSuperview().offset(10)
                   make.width.equalTo(60)
                   make.height.equalTo(30)
                   make.top.equalTo(10)
               }
               
               self.addSubview(self.rightButton)
               self.rightButton.snp.makeConstraints { (make) in
                   make.right.equalToSuperview().offset(10)
                   make.top.equalTo(5)
                   make.height.equalTo(40)
                   make.width.equalTo(70)
               }
        
        self.addSubview(collectionView)
    }
}

extension RecommendNewsCollectionViewCell:UICollectionViewDelegate,UICollectionViewDataSource{
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 100
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell:NoticCell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! NoticCell
        return cell
    }
    
    func startTimer() {
        let time = Timer.init(timeInterval: 2, target: self, selector: #selector(nextPage), userInfo: nil, repeats: true)
        RunLoop.main.add(time, forMode: .common)
        self.timer = time
    }
   @objc func nextPage() {
    let currentOffsetY = collectionView.contentOffset.y
    let offsetY = currentOffsetY + collectionView.bounds.height
    collectionView.setContentOffset(CGPoint.init(x: 0, y: offsetY), animated: true)
   }
    
    func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
        self.timer?.invalidate()
        self.timer = nil
    }
    func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
         startTimer()
    }
}

class NoticCell: UICollectionViewCell {
    lazy var titleLabel:UILabel = {
        let label = UILabel.init()
        label.font = UIFont.systemFont(ofSize: 14)
        label.textColor = UIColor.red
        label.text = "我是一个测试轮播"
        return label
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
